package com.knife.bbs.action;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.knife.bbs.business.ActiveUser;
import com.knife.bbs.business.BBSDirService;
import com.knife.bbs.business.BBSRights;
import com.knife.bbs.object.BBSDir;
import com.knife.util.CommUtil;
import com.knife.web.ActionContext;
import com.knife.web.Module;
import com.knife.web.Page;
import com.knife.web.WebForm;

/**
 * 
 * <p>
 * Title:BBS栏目处理类
 * </p>
 * <p>
 * Description: BBS目录的列表处理Action,使用KnifeWeb. <br>
 * 通过继承com.knife.web.tools.AbstractCmdAction,实现简单的命令式Action处理,去除了复杂烦锁的if else语句
 * </p>
 */
public class BBSDirAction extends BBSAction {
	private BBSDirService dirService = BBSDirService.getInstance();

	public Page doInit(WebForm form, Module module) {
		return doMenu(form, module);
	}

	/**
	 * 显示菜单
	 * 
	 * @param form
	 * @param module
	 * @return 菜单Page
	 */
	public Page doMenu(WebForm form, Module module) {
		ActiveUser user = (ActiveUser) ActionContext.getContext().getSession()
				.getAttribute("bbsuser");
		doQuery(form, module);
		if (BBSRights.checkActiveUser(user)) {
			form.addResult("activeuser", "1");
		}
		if (BBSRights.checkAdministrator(user)) {
			form.addResult("administrator", "1");
		}
		return module.findPage("/"+skin+"/","menu");
	}

	public Page doTreeMenu(WebForm form, Module module) {
		String sn = CommUtil.null2String(form.get("sn"));
		// String parent=CommUtil.null2String(form.get("parent"));
		List list = new ArrayList();
		List rootList = null;

		if (sn.equals(""))// 读取根目录下的主栏目
		{
			rootList = dirService.getRootDirs();
		} else {
			BBSDir pDir = dirService.getBBSDirBySN(sn);
			rootList = dirService.getChildDirs(pDir);
		}
		if (rootList != null) {
			for (int i = 0; i < rootList.size(); i++) {
				BBSDir dir = (BBSDir) rootList.get(i);
				Map map = new HashMap();
				map.put("title", dir.getTitle());
				map.put("sn", dir.getSn());
				if (dirService.getChildDirs(dir).size() > 0)
					map.put("more", "true");
				list.add(map);
			}
		}
		form.addResult("list", list);
		return new Page("treeXml", "/manage/xmlDirTree.xml", "template");
	}

	/**
	 * 增加新栏目
	 * 
	 * @param form
	 * @param module
	 * @return 目录编辑Page
	 */
	public Page doNew(WebForm form, Module module) {
		ActiveUser user = (ActiveUser) ActionContext.getContext().getSession()
				.getAttribute("bbsuser");
		if (!BBSRights.checkAdministrator(user))
			return new Page("popedomError", "norights.htm", "html","/"+skin+"/");
		return module.findPage("/"+skin+"/","edit");
	}

	/**
	 * 目录列表
	 * 
	 * @param form
	 * @param module
	 * @return 列表Page
	 */
	public Page doList(WebForm form, Module module) {
		ActiveUser user = (ActiveUser) ActionContext.getContext().getSession()
				.getAttribute("bbsuser");
		if (!BBSRights.checkAdministrator(user))
			return new Page("popedomError", "norights.htm", "html","/"+skin+"/");
		return doQuery(form, module);
	}

	/**
	 * 增选栏目添加操作
	 * 
	 * @param form
	 * @param module
	 * @return 添加成功返回列表Page,失败返回编辑Page
	 */
	public Page doAdd(WebForm form, Module module) {
		ActiveUser user = getCurrentUser();
		com.knife.bbs.domain.BBSDir dir = new com.knife.bbs.domain.BBSDir();
		if (dir != null) {
			// 以下执行表单对象VO到PO转换的代码,在验证框架中可以省略
			dir.setSn(CommUtil.null2String(form.get("sn")));
			if (!BBSRights.checkRights(dir, "add", user))
				return new Page("popedomError", "norights.htm", "html","/"+skin+"/");
			if (dirService.getBBSDirBySN(dir.getSn()) != null) {
				form.addResult("msg", "栏目编号不能重复！");
				return module.findPage("edit");
			}
			form.toPo(dir);
			if (dir.getParentsn() != null && (!dir.getParentsn().equals("")))// 处理dirPath
			{
				BBSDir d1 = dirService.getBBSDirBySN(dir.getParentsn());
				if (d1 != null)
					dir.setDirpath(d1.getDirpath() + "@" + dir.getSn());
			} else{
				dir.setDirpath("root@" + dir.getSn());
			}
			dir.setInputtime(new Date());
			dir.setStatus(new Integer(0));
			boolean op = dirService.add(dir);
			if (op) {
				form.addResult("msg", "栏目添加成功！");
			} else {
				form.addResult("msg", "栏目添加失败");
			}
		} else {
			form.addResult("msg", "所要修改的目录不存在！");
		}
		return doQuery(form, module);
	}

	/**
	 * 修改栏目操作
	 * 
	 * @param form
	 * @param module
	 * @return 操作成功返回列表Page,失败返回编辑Page
	 */
	public Page doUpdate(WebForm form, Module module) {
		String cid = CommUtil.null2String(form.get("cid"));
		ActiveUser user = getCurrentUser();
		BBSDir dir = dirService.getBBSDir(cid);
		if (dir != null) {
			// 在Action中检测权限
			if (!BBSRights.checkRights(dir, "update", user)){
				return new Page("popedomError", "norights.htm", "html","/"+skin+"/");
			}
			// 以下执行表单对象VO到PO转换的代码,在验证框架中可以省略
			com.knife.bbs.domain.BBSDir adir=new com.knife.bbs.domain.BBSDir();
			form.toPo(adir);
			dirService.update(adir);
		} else {
			form.addResult("msg", "所要修改的目录不存在！");
		}
		return doQuery(form, module);
	}

	/**
	 * 编辑数据,从持久层读取数据显示到VO层
	 * 
	 * @param form
	 * @param module
	 * @return 返回编辑Page
	 */
	public Page doEdit(WebForm form, Module module) {
		String cid = CommUtil.null2String(form.get("cid"));
		ActiveUser user = getCurrentUser();
		BBSDir dir = dirService.getBBSDir(cid);
		if (!BBSRights.checkRights(dir, "edit", user)){
			return new Page("popedomError", "norights.htm", "html","/"+skin+"/");
		}
		if (dir != null){
			form.addPo(dir);
		}
		return module.findPage("/"+skin+"/","edit");
	}

	/**
	 * 目录删除操作,根据主键cid删除相应目录
	 * 
	 * @param form
	 * @param module
	 * @return 返回目录列表页
	 */
	public Page doDel(WebForm form, Module module) {
		String cid = CommUtil.null2String(form.get("cid"));
		ActiveUser user = getCurrentUser();
		BBSDir dir = dirService.getBBSDir(cid);
		if (dir != null) {
			if (!BBSRights.checkRights(dir, "del", user)){
				return new Page("popedomError", "norights.htm", "html","/"+skin+"/");
			}
			com.knife.bbs.domain.BBSDir adir=new com.knife.bbs.domain.BBSDir();
			adir.setDir(dir);
			dirService.del(adir);
			form.addResult("msg", "删除成功！");
		}
		return doQuery(form, module);
	}

	/**
	 * 列出所有目录
	 * 
	 * @param form
	 * @param module
	 * @return 返回列表Page
	 */
	public Page doQuery(WebForm form, Module module) {
		String parentSn = (String) form.get("parentsn");
		if (parentSn != null && (!parentSn.equals(""))) {
			BBSDir dir = dirService.getBBSDirBySN(parentSn);
			if (dir != null)
				form.addResult("list", dirService.getChildDirs(dir));
		} else
			form.addResult("list", dirService.getRootDirs());
		return module.findPage("/"+skin+"/","list");
	}

	/**
	 * 通过Session取得当前操作用户
	 * 
	 * @return ActiveUser
	 */
	public ActiveUser getCurrentUser() {
		ActiveUser user = (ActiveUser) ActionContext.getContext().getSession()
				.getAttribute("bbsuser");
		return user;
	}
}
